<?php
/**
* @package      auto_occasion
* @subpackage   admin
* @author       CMS
* @copyright    2011
* @license      All rights reserved
*/

class BoCompteCtrl extends jController {

    
    public $pluginParams = array(
                                    '*'=>array('auth.required'=>true,
                                    'jacl2.right'=>'admin.compte.gerer'
                                    )
                            );


    /**
    * Affiche la vue par defaut dans le BO, liste des comptes
    */
    function index() {
        $rep = $this->getResponse('html');
        $rep->menusActifs = adminHtmlResponse::MENU_OPERATEURS ;

		$rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jconfirmaction.jquery.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jconfirmaction.jquery.ask.js');


        jClasses::inc('admin~compteSrv');
        $users = compteSrv::liste();

        $rep->bodyTpl = 'admin~BoCompteList';
        
        $rep->body->assign('users', $users);

        return $rep;
    }

    /**
    * Edition / ajout d'un compte
    */
    function editer() {
        $rep = $this->getResponse('html');
        $rep->menusActifs = adminHtmlResponse::MENU_OPERATEURS ;
        
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/compte.css');
   		$rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.validation.js');



        jClasses::inc('admin~compteSrv');
        jClasses::inc('admin~profilSrv');


        $login  = $this->param('login', '');

        $user   = ($login) ? compteSrv::chargeParLogin($login) : compteSrv::init();
        $profil = profilSrv::liste();

        $rep->body->assign('login', $login);
        $rep->body->assign('user', $user);
        $rep->body->assign('profil', $profil);

        $rep->bodyTpl = 'admin~BoCompteEdit';
        
        return $rep;
    }

    /**
    * Sauvegarder un compte
    */
    function sauvegarder() {
        $rep = $this->getResponse('redirect');
        
        jClasses::inc('admin~compteSrv');

        $compte          = new stdClass();
        $compte->login   = $this->stringParam('login','');
        $compte->mdp     = $this->stringParam('mdp','');
        $compte->profil  = $this->stringParam('profil','');
        $compte->email   = $this->stringParam('email','');
        $compte->id      = $this->param('id','');

        if ($compte->id) {
            compteSrv::modifier($compte);
        } else {
            compteSrv::ajouter($compte);
        }
        
        $rep->action = 'admin~BoCompte:index';

        return $rep;
    }

    /**
    * Supprime un compte
    */
    function supprimer() {
        $rep = $this->getResponse('redirect');
        jClasses::inc('admin~compteSrv');

        $compteId = $this->stringParam('login');
        compteSrv::supprimer($compteId);

        $rep->action = 'admin~BoCompte:index';

        return $rep;
    }

}

